package com.gz.jdbc.datasource;

import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class JdbcUtilsByDruid_use {


    @Test//为防止Connection关闭，原来的ResultSet就无法使用，用土方法(即用ArrayList保存数据)
    public void test_druid_utils_use(){
//        String sql ="ALTER TABLE bank rename column b1 to score" ;

        String sql = "select * from bank where id>=?";
        ResultSet set = null;
        PreparedStatement ps = null;
        Connection conn = null;
        ArrayList<Table> list = new ArrayList<>();

        try {
            conn=JdbcUtilsByDruid.getConnection();
            System.out.println(conn.getClass());//class com.alibaba.druid.pool.DruidPooledConnection
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 1);
            set = ps.executeQuery();
//            ps.executeUpdate();
            System.out.println("使用我的DruidUtils");
            while (set.next()) {
                int id = set.getInt("id");
                int score = set.getInt("score");
                System.out.println(id+"\t"+score);
                list.add(new Table(id, score));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtilsByDruid.close(set,ps,conn);
        }
        for (Table table : list) {
            System.out.println("id:"+table.getId()+",score:"+table.getScore());
        }
    }
}
